package com.beanbean.myaudiorecord;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.beanbean.record.AudioManager;
import com.beanbean.record.AudioRecorder;
import com.beanbean.record.FileUtil;

import java.io.File;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private final static String TAG = "RecordMainActivity";
    private Context mContext;
    private String filePath;
    private AudioRecorder mAudioRecorder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        init();
    }

    private void init() {
        filePath = System.currentTimeMillis()+"_sample.wav"; //dummy file
        mAudioRecorder = new AudioRecorder(mContext);
        mAudioRecorder.createDefaultAudio(filePath);
        findViewById(R.id.start).setOnClickListener(this);
        findViewById(R.id.pause).setOnClickListener(this);
        findViewById(R.id.stop).setOnClickListener(this);
        findViewById(R.id.wavPlayer).setOnClickListener(this);

        AudioManager.getInstance(FileUtil.getDirPath()).setOnAudioStageListener(new AudioManager.AudioStageListener() {
            @Override
            public void wellPrepared() {
                Log.i(TAG, "wellPrepared: ");
            }

            @Override
            public void wellFinish(String path) {
                Log.i(TAG, "wellFinish: "+path);
            }
        });

    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.start:
               // mAudioRecorder.startRecord();
                AudioManager.getInstance(FileUtil.getDirPath()).prepareAudio();
                break;
            case R.id.pause:
                break;
            case R.id.stop:
               // mAudioRecorder.stopRecord();
                AudioManager.getInstance(FileUtil.getDirPath()).release();
                break;
            case R.id.wavPlayer:
                playFile();
                break;
            default:

                break;


        }
    }

    private void playFile() {
        String path = mAudioRecorder.getAudioPath();
        Log.i(TAG, "playFile: " + path);
        if (path != null && path.length() != 0) {
            Intent intent = new Intent();
            intent.setAction(android.content.Intent.ACTION_VIEW);
            File file = new File(path);
            Uri videoURI = FileProvider.getUriForFile(mContext, mContext.getApplicationContext().getPackageName() + ".provider", file);
            intent.setDataAndType(videoURI, "audio/*");
            startActivity(intent);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
